package edu.task.repository;

import edu.task.model.SysMenu;
import edu.task.model.SysProject;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SysProjectRepository extends JpaRepository<SysProject, Long> {

    final String JOIN_USER_ROLE=" left join sys_user_project up on up.project_id = p.project_id ";
    final String JOIN_USER=" left join sys_user u on u.user_id = up.user_id ";

    @Modifying
    @Query(value = " delete from sys_project where project_id=:projectId ", nativeQuery = true)
    int deleteProjectById(Long projectId);

    @Query(value = "select p.project_id from sys_project p " +JOIN_USER_ROLE+JOIN_USER+
            "WHERE u.user_id=:userId",nativeQuery = true)
    List<Long> selectProjectListByUserId(Long userId);

    @Query(value = "select p.project_id from sys_project as p " +
            "inner join sys_project_task as pr on p.project_id = pr.project_id " +
            "inner join sys_task as t on pr.task_id = t.task_id " +
            "WHERE t.task_id=:taskId",nativeQuery = true)
    List<Long> selectProjectByTaskId(List<Long> taskId);
}
